﻿using AutoMapper;
using B.S.Filtration.Domain.到货检验;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Write.Api.Application.Command.到货检验.BasicInfoCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Write.Api.Application.CommandHander.到货检验.BasicInfoHandler
{
    public class AddBasicInfoHandler : IRequestHandler<AddBasicInfoCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public AddBasicInfoHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<APIResult<object>> Handle(AddBasicInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var model = mapper.Map<BasicInfo>(request);
            string yeartime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string ProductCodess1 = "JYDH" + yeartime;
            model.InspectionOrderNumber = ProductCodess1;
            model.IsDel = false;

            result.data = await db.Insertable(model).ExecuteCommandAsync();
            if (result.data != null)
            {
                result.code = APIResultCode.success;
                result.msg = "到货检验信息添加成功";
            }
            else
            {
                result.code = APIResultCode.failed;
                result.msg = "到货检验信息添加失败";
            }
            return result;
        }
    }
}
